﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using devtm.Helpers;
using System.Configuration;

namespace devtm.Editor.Configuration
{
    public class BoxSetting
    {

        private Attribute _attr;

        private PropertyInfo pkey = null;
        private PropertyInfo pValue = null;
        private PropertyInfo pValueExtended = null;
        private PropertyInfo pDisplay = null;


        public static IEnumerable<BoxSetting> GetSettingAttribute(PropertyInfo propertyInfo)
        {
            Attribute[] attr = Attribute.GetCustomAttributes(propertyInfo, true);
            
            foreach (Attribute item in attr)
                if (item.GetType().IsOfType(typeof(SettingAttribute)))
                    yield return new BoxSetting(item);
            
        }


        public static IEnumerable<BoxSetting> GetBoxSetting(PropertyInfo propertyInfo, string name)
        {

            foreach (BoxSetting item in GetSettingAttribute(propertyInfo))
                if (item.Key == name)
                    yield return item;
                       
        }


        public BoxSetting(Attribute attr)
        {
            _attr = attr;
            pkey = attr.GetType().GetProperty("Key");
            pValue = attr.GetType().GetProperty("Value");
            pValueExtended = attr.GetType().GetProperty("ValueExtended");
            pDisplay = attr.GetType().GetProperty("Display");
        }

        public string Key { get { return (string)pkey.GetValue(_attr, null); } }
        public string Display { get { return (string)pDisplay.GetValue(_attr, null); } }
        public Object Value { get { return pValue.GetValue(_attr, null); } }
        public string ValueString { get { return pValue.GetValue(_attr, null).ToString(); } }
        public Object ValueExtended { get { return pValueExtended.GetValue(_attr, null); } }


        public Type Type { get { return Value.GetType(); } }

    }
}
